package com.mcx.modules.proto;

import com.mcx.modules.common.IProto;
import com.mcx.modules.common.MMO_DataStream;

import java.io.*;
import java.util.*;

/**
 * 服务器返回任务列表消息
 */
public class Task_SearchTaskReturnProto implements IProto
{
    private static MMO_DataStream dataStream = new MMO_DataStream();

    @Override
    public short getProtoCode() { return 15002; }

    public Integer TaskCount; //任务数量
    public ArrayList<TaskItem> CurrTaskItemList; //任务项

    /**
     * 任务项
     */
    public static class TaskItem
    {
        public Integer Id; //任务编号
        public String Name; //任务名称
        public Integer Status; //任务状态
        public String Content; //任务描述
    }

    public byte[] ToArray() {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try (DataOutputStream outputStream = new DataOutputStream(out)) {
            dataStream.outputStream = outputStream;
            dataStream.WriteShort(getProtoCode());
            dataStream.WriteInt(TaskCount);
            for (int i = 0; i < TaskCount; i++) {
                dataStream.WriteInt(CurrTaskItemList.get(i).Id);
                dataStream.WriteUTF8String(CurrTaskItemList.get(i).Name);
                dataStream.WriteInt(CurrTaskItemList.get(i).Status);
                dataStream.WriteUTF8String(CurrTaskItemList.get(i).Content);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return out.toByteArray();    }
    public static Task_SearchTaskReturnProto GetProto(byte[] buffer) {
        Task_SearchTaskReturnProto proto = new Task_SearchTaskReturnProto();
        try (DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(buffer))) {
            dataStream.inputStream = inputStream;
            proto.TaskCount = dataStream.ReadInt();
            proto.CurrTaskItemList = new ArrayList<TaskItem>();
            for (int i = 0; i < proto.TaskCount; i++) {
                TaskItem _CurrTaskItem = new TaskItem();
                _CurrTaskItem.Id = dataStream.ReadInt();
                _CurrTaskItem.Name = dataStream.ReadUTF8String();
                _CurrTaskItem.Status = dataStream.ReadInt();
                _CurrTaskItem.Content = dataStream.ReadUTF8String();
                proto.CurrTaskItemList.add(_CurrTaskItem);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return proto;
    }
}